//
//  AlertBuildAlume.m
//  u76ho
//
//  Created by m-air-01 on 16/1/12.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "AlertBuildAlume.h"
@interface AlertBuildAlume ()<UITextFieldDelegate>
{
    NSString * title;
    NSString * content;
    NSString * btnName;
    
    UIImageView * _imageView;
    
    NSString * str;
}

@end
@implementation AlertBuildAlume
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)text content:(NSString *)contentStr btnName:(NSString *)BtnStr
{
    self = [super initWithFrame:frame];
    if (self) {
        str = nil;
        title = text;
        content = contentStr;
        btnName = BtnStr;
        [self addScreenShot];
        [self addAlertView];
    }
    return self;
}
-(void)addScreenShot{
    _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    UIImage * image = [UIImage imageScreenView:RootWindow];
    _imageView.image = image;
    UIView * view = [[UIView alloc]initWithFrame:self.bounds];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.4;
    [_imageView addSubview:view];
    [self addSubview:_imageView];
}
-(void)addAlertView
{
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(23), SCREEN_HEIGHT/2-200/2, SCREEN_WIDTH-SIZETO6W(23)*2, 200)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.userInteractionEnabled = YES;
    [self addSubview:bgView];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 23, bgView.width, 20)];
    titleLab.text = title;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = UIColorFromRGB(0x333944);
    [bgView addSubview:titleLab];
    
    UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(0, titleLab.bottom+10, bgView.width, 20)];
    contentLab.text = content;
    contentLab.textColor = UIColorFromRGB(0x616978);
    contentLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:contentLab];
    
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(SIZETO6W(32), contentLab.bottom+10, bgView.width-SIZETO6W(32)*2, 44)];
    textField.delegate = self;
    textField.placeholder = @"  标题";
    textField.layer.masksToBounds = YES;
    textField.layer.borderColor = [UIColorFromRGB(0x94999f) CGColor];
    textField.layer.borderWidth = 1.0f;
    textField.tag = 504;
    [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [bgView addSubview:textField];
    
    CGFloat btnWidth = bgView.width - SIZETO6W(32)*2;
    
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SIZETO6W(32), bgView.height-25 - 41, btnWidth, 41);
    [btn setBackgroundColor:UIColorFromRGB(0x6ec844)];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:btnName forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 20;
    [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    [bgView addSubview:btn];
    
    UIImageView * closeImg = [[UIImageView alloc]initWithFrame:CGRectMake(bgView.width-SIZETO6W(19)-SIZETO6W(35), 0, 35, 40)];
    closeImg.userInteractionEnabled = YES;
    closeImg.image = [UIImage imageNamed:@"close.png"];
    [bgView addSubview:closeImg];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [closeImg addGestureRecognizer:tap];
    
    [self animation:bgView addSize:YES];
}
-(void)textChange:(UITextField *)textField
{
    str = textField.text;
}
-(void)btnAction:(UIButton *)btn
{
    
    
    UITextField * textField = (UITextField *)[self viewWithTag:504];
    
    if ([self isEmpty:textField.text]) {
        textField.text = @"";
        return ;
    }
    
    if ([textField.text isEqualToString:@""]) {
        [Singleton alerSendMessage:@"请输入相册名字"];
    }
    else
    {
        [self removeFromSuperview];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"createAlumeNotificase" object:textField.text];
    }
}
-(BOOL) isEmpty:(NSString *) sstr {
    
    if (!sstr) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [sstr stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        //        NSLog(@"开始登录了");
        
        UITextField * textField = (UITextField *)[self viewWithTag:504];
        
        if ([self isEmpty:textField.text]) {
            textField.text = @"";
        }
        
        if ([textField.text isEqualToString:@""]) {
            [Singleton alerSendMessage:@"请输入相册名字"];
        }
        else
        {
            [self removeFromSuperview];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"createAlumeNotificase" object:textField.text];
        }
        return NO;
    }
    return YES;
    
    
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    [self removrFromeSuperViewers];
}
-(void)animation:(UIView *)view addSize:(BOOL)is
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.2f;
    animation.repeatCount = 1;
    
    if (is) {
        animation.autoreverses = NO;
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        [view.layer addAnimation:animation forKey:@"alertyes"];
    }else{
        animation.autoreverses = YES;
        animation.fromValue = [NSNumber numberWithFloat:1.0f];
        animation.toValue = [NSNumber numberWithFloat:0.0f];
        [view.layer addAnimation:animation forKey:@"alertno"];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸相册的操作开始列");
    UITextField * textField = (UITextField *)[self viewWithTag:504];
    [textField resignFirstResponder];
    
}
-(void)removrFromeSuperViewers
{
    _imageView.hidden = YES;
    [self animation:self addSize:NO];
    
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
